#!/usr/bin/env python
#!coding=utf-8

from PySide.QtGui import *
from PySide.QtCore import *

import sys
from form import Ui_Form
from greecket.qtfactory  import *
from greecket.greepeer import CollaboratePeer

#from qtfactory import *

class MForm(QWidget):
    def __init__(self):
        super(MForm,self).__init__()
        self.form=Ui_Form()
        self.form.setupUi(self)
        self._sckp=Greecket.getFactory()#self)#,('localhost',5423))
        self._ps=[]

        #self._sckp.bind(GREE_DATARECEIVED,self.OnDataReceived)
        
        #self._sckp.bind(GREE_CONNECTIONLOST,self.OnConnectionLost)
        #self._sckp.bind(GREE_LISTEN,self.OnListen)
        #self._sckp.bind(GREE_ERROR,self._socketerror)
        self._Peer=CollaboratePeer()
        self._Peer.bind(GREE_LISTEN,self.OnListen)
        self._Peer.bind(GREE_ONLINE,self._OnLine)
        #self._Peer.bind(GREE_CONNECTIONMADE,self._connectionMade)
        #self._Peer.bind(GREE_CONNECTIONMADE,self._connectionMade)
        self.form.Run.clicked.connect(self.OnbtRunClick)
        self.form.Send.clicked.connect(self.OnbtSend)

    def _OnLine(self,evt):
        self.form.OutText.appendPlainText('++: %s online.' % evt.Data.getName())
        evt.Data.bind(GREE_DATARECEIVED,self.OnDataReceived)
        self._ps.append(evt.Data)

    def OnDataReceived(self,evt):
        self.form.OutText.appendPlainText('>>: %s' % (evt.Data.decode('utf-8','replace')))

    def OnConnectionLost(self,evt):
        print evt.Data
        a=evt.Peer.getPeerName()
        self.form.OutText.appendPlainText('--: %s:%s' % a)
                
    def OnListen(self,evt):
        self.form.OutText.appendPlainText('==: %s.' % str(evt))

    def doRun(self,AsServer):
        d=(self.form.Host.text(),int(self.form.Port.text()))
        self.Address=d
        if AsServer:
            self._Peer.listen(d)
            
        else:
            self._Peer.connect(d)
            #s.bind(GREE_CONNECTIONMADE,self._connectionMade)

    def OnbtRunClick(self):
        self.doRun(self.form.AsService.checkState()==Qt.Checked)
        

    def OnbtSend(self):
        for p in self._ps:
            p.send(self.form.InputText.toPlainText().encode('utf-8','replace'))
        self.form.OutText.appendPlainText('<<: %s' % (self.form.InputText.toPlainText()))
        self.form.InputText.setPlainText('')


    def closeEvent(self,evt):
        #print '**: Exit\n'
        #self._sckp.stop()
        #print self._gfc.isAlive()
        self._sckp=None
        self._gfc=None
        #if self._sckp:
            


    def _connectionMade(self,evt):
        a=evt.Data.getName()#Peer.get
        self._ps.append(evt.Data)
        self.form.OutText.appendPlainText('++: %s\n' % a)

    def _socketerror(self,evt):
        #print [evt.Data]
        self.form.OutText.appendPlainText('**: %s.\n' % evt.Data)



        

if __name__=='__main__':
    app=QApplication(sys.argv)
    Greecket(collaborate=True)#self)#,('localhost',5423))
    #Collaborate()

    mwin=MForm()
    mwin.setWindowTitle(u'greecket chat test')
    mwin.show()
    sys.exit(app.exec_())

    
